package com.yisu.kabao.manager.core.service.impl;

import com.yisu.kabao.ewf.common.web.EasyUIGrid;
import com.yisu.kabao.ewf.entity.GoodsType;
import com.yisu.kabao.manager.core.mapper.GoodsTypeMapper;
import com.yisu.kabao.manager.core.service.IGoodsTypeService;
import com.yisu.kabao.manager.domain.request.baseInfoPackage.GoodsCategoryRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：商品分类业务实现类
 * createTime：2017/8/9
 */
@Service
public class GoodsTypeServiceImpl extends BaseService implements IGoodsTypeService {
    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    /**
     * author:赵杰
     * Business Description : 分页获取商品分类
     *
     * @param goodsCategoryRequest 分页请求信息
     * @return 商品分类列表
     */
    @Override
    public EasyUIGrid<GoodsType> getGoodsTypeByPage(GoodsCategoryRequest goodsCategoryRequest) {
        List<GoodsType> biReportProducts = goodsTypeMapper.getGoodsTypeByPage(goodsCategoryRequest);
        EasyUIGrid<GoodsType> gridData = new EasyUIGrid<>();
        gridData.setTotal(goodsCategoryRequest.getTotalRecords());
        gridData.setRows(biReportProducts);
        return gridData;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，获取商品分类
     *
     * @param primaryKey 主键
     * @return
     */
    @Override
    public GoodsType getGoodsType(long primaryKey) {
        try {
            return goodsTypeMapper.selectByPrimaryKey(primaryKey);
        } catch (Exception e) {
            logger.error(" 通过主键ID，获取商品分类异常：" + e.getMessage());
        }
        return null;
    }

    /**
     * author:赵杰
     * Business Description : 编辑商品分类信息
     *
     * @param goodsType 商品分类信息
     * @return
     */
    @Override
    public boolean modifyGoodsType(GoodsType goodsType) {
        try {
            GoodsType checkModel = goodsTypeMapper.selectByPrimaryKey(goodsType.getId());
            if (checkModel == null) {
                goodsType.setCreateDate(new Date());
                goodsType.setIsValid(true);
                return goodsTypeMapper.insertSelective(goodsType) > 0;
            } else {
                GoodsType updateModel = new GoodsType();
                updateModel.setUpdateDate(new Date());
                updateModel.setSort(goodsType.getSort());
                updateModel.setName(goodsType.getName());
                updateModel.setDescrib(goodsType.getDescrib());
                updateModel.setIco(goodsType.getIco());
                updateModel.setId(goodsType.getId());
                return goodsTypeMapper.updateByPrimaryKeySelective(updateModel) > 0;
            }
        } catch (Exception e) {
            logger.error("编辑商品分类信息异常：" + e.getMessage());
        }
        return false;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，删除商品分类信息
     *
     * @param primaryKey 主键ID
     * @return
     */
    @Override
    public boolean deleteGoodsType(long primaryKey) {
        try {
            GoodsType checkModel = goodsTypeMapper.selectByPrimaryKey(primaryKey);
            if (checkModel != null) {
                GoodsType updateModel = new GoodsType();
                updateModel.setIsValid(false);
                updateModel.setId(primaryKey);
                return goodsTypeMapper.updateByPrimaryKeySelective(updateModel) > 0;
            }
        } catch (Exception e) {
            logger.error("通过主键ID，删除商品分类信息异常：" + e.getMessage());
        }
        return false;
    }
}
